<!--
 * @Author: hq
 * @Date: 2022-05-24 14:32:30
 * @LastEditors: hq
 * @LastEditTime: 2022-07-12 21:12:39
 * @Description: your project
 * @version: 1.0
-->
<template>
  <div class="manages">
    <el-form
      :model="ruleForm"
      :rules="rules"
      ref="ruleForm"
      label-width="100px"
      class="demo-ruleForm"
    >
      <el-form-item label="角色" prop="role_id">
        <el-select v-model="ruleForm.role_id" placeholder="请选择角色">
          <el-option
            v-for="item in roleArr"
            :key="item.id"
            :label="item.name"
            :value="item.id"
          >
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="用户名" prop="username">
        <el-input
          placeholder="请输入用户名"
          v-model="ruleForm.username"
          autocomplete="off"
        ></el-input>
      </el-form-item>
      <el-form-item label="密码">
        <el-input
          type="password"
          placeholder="请输入密码"
          v-model="ruleForm.psw"
          autoComplete="new-password"
        ></el-input>
      </el-form-item>
      <el-form-item label="确认密码">
        <el-input
          type="password"
          placeholder="请输入确认密码"
          v-model="ruleForm.confirmPsw"
        ></el-input>
      </el-form-item>
      <el-form-item label="昵称" prop="nickname">
        <el-input
          placeholder="请输入昵称"
          v-model="ruleForm.nickname"
        ></el-input>
      </el-form-item>
      <el-form-item label="手机号" prop="mobile">
        <el-input
          type="number"
          placeholder="请输入手机号"
          v-model="ruleForm.mobile"
        ></el-input>
      </el-form-item>
      <el-form-item label="邮箱" prop="email">
        <el-input placeholder="请输入邮箱" v-model="ruleForm.email"></el-input>
      </el-form-item>
      <el-form-item label="状态" :required="true">
        <el-switch
          active-text="禁用"
          inactive-text="启用"
          :active-value="1"
          :inactive-value="0"
          class="tablescope"
          v-model="ruleForm.status"
        >
        </el-switch>
      </el-form-item>
    </el-form>
    <div class="btn">
      <el-button type="primary" @click="handleSubmit">确 定</el-button>
    </div>
  </div>
</template>

<script>
import { adminList, addAccount, editAccount } from "@/api/manager";
import md5 from "js-md5";
export default {
  props: ["rowData"],
  data() {
    return {
      ruleForm: {
        role_id: "",
        username: "",
        psw: "",
        confirmPsw: "",
        nickname: "",
        mobile: "",
        email: "",
        status: 1,
      },
      roleArr: [],
      rules: {
        role_id: [{ required: true, message: "请选择角色", trigger: "change" }],
        username: [
          { required: true, message: "请输入管理员账号", trigger: "blur" },
        ],

        nickname: [{ required: true, message: "请输入昵称", trigger: "blur" }],
        mobile: [{ required: true, message: "请输入手机号", trigger: "blur" }],
        email: [{ required: true, message: "请输入邮箱", trigger: "blur" }],
      },
    };
  },
  methods: {
    //角色下拉框
    async adminList() {
      const { data, err } = await adminList({});
      if (err === 0) {
        this.roleArr = data.data;
        if (this.rowData.id) {
          console.log(this.rowData);
          //编辑
          this.ruleForm.role_id = this.rowData.role_id;
          this.ruleForm.username = this.rowData.username;
          this.ruleForm.nickname = this.rowData.nick;
          this.ruleForm.mobile = this.rowData.mobile;
          this.ruleForm.email = this.rowData.email;
          this.ruleForm.status = this.rowData.status;
        }
      }
    },
    //添加
    async addAccount() {
      const { msg, err } = await addAccount({
        role_id: this.ruleForm.role_id,
        username: this.ruleForm.username,
        password: md5(this.ruleForm.psw),
        confirmPsw: md5(this.ruleForm.confirmPsw),
        nick: this.ruleForm.nick,
        mobile: this.ruleForm.mobile,
        email: this.ruleForm.email,
        status: this.ruleForm.status,
      });
      if (err === 0) {
        this.$message({
          showClose: true,
          message: "操作成功",
          type: "success",
        });
        this.$emit("handleClose");
      } else {
        this.$message({
          showClose: true,
          message: msg,
          type: "error",
        });
      }
    },
    //编辑
    async editAccount() {
      const { msg, err } = await editAccount({
        id: this.rowData.id,
        role_id: this.ruleForm.role_id,
        username: this.ruleForm.username,
        password: md5(this.ruleForm.psw),
        confirmPsw: md5(this.ruleForm.confirmPsw),
        nick: this.ruleForm.nick,
        mobile: this.ruleForm.mobile,
        email: this.ruleForm.email,
        status: this.ruleForm.status,
      });
      if (err === 0) {
        this.$message({
          showClose: true,
          message: "操作成功",
          type: "success",
        });
        this.$emit("handleClose");
      } else {
        this.$message({
          showClose: true,
          message: msg,
          type: "error",
        });
      }
    },

    handleSubmit() {
      this.$refs.ruleForm.validate((valid) => {
        if (valid) {
          if (
            this.ruleForm.psw &&
            this.ruleForm.psw !== this.ruleForm.confirmPsw
          ) {
            this.$message({
              showClose: true,
              message: "确认密码与密码不一致",
              type: "warning",
            });
          } else {
            if (this.rowData.id) {
              //编辑
              this.editAccount();
            } else {
              //新增
              this.addAccount();
            }
          }
        } else {
          return false;
        }
      });
    },
  },
  created() {
    this.adminList();
  },
};
</script>

<style lang="scss" scoped>
.manages {
  padding: 0 30px;
  box-sizing: border-box;
}
</style>
